package com.han.verifycode.ver;

import com.han.verifycode.CodeConfig;
import com.han.verifycode.VerifyCode;
import com.han.verifycode.data.Answer;
import com.han.verifycode.data.RightKey;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public interface ExactMatchCode extends VerifyCode {
    @Override
    default boolean verify(RightKey righKey, Answer answer) {
        if (righKey == null || righKey.getAnswer() == null) {
            return false;
        }
        CodeConfig config = righKey.getConfig();
        Long expTime = config.getExpireTime();
        LocalDateTime createDate = righKey.getCreateDate();
        if (expTime != null && expTime > 0 && createDate != null) {
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime expireAt = createDate.plus(expTime, ChronoUnit.SECONDS);
            if (now.isAfter(expireAt)) {
                //验证码已过期
                return false;
            }
        }
        String rightAnswer = righKey.getAnswer();
        return rightAnswer.equalsIgnoreCase(answer.getAnswer());
    }
}
